import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class TestUserManagement:
    @pytest.mark.normal
    @pytest.mark.usermanagement
    @pytest.mark.parametrize('userinfo', YamlUtil().read_testcase_yaml('v24.09/userManagement/4_get_user_list.yaml'))
    def test_get_user_list(self, userinfo):
        # 获取用户列表接口-正常
        base_url = datas.baseUrl
        url = base_url + userinfo['request']['url']
        method = userinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.usermanagement
    @pytest.mark.parametrize('userinfo', YamlUtil().read_testcase_yaml('v24.09/userManagement/1_create_user.yaml'))
    def test_create_user(self, userinfo):
        # 创建用户接口-正常
        base_url = datas.baseUrl
        url = base_url + userinfo['request']['url']
        method = userinfo['request']['method']
        data = userinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        globals()['username'] = req.json()['Data']['Username']
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.usermanagement
    @pytest.mark.parametrize('userinfo', YamlUtil().read_testcase_yaml('v24.09/userManagement/3_modify_user.yaml'))
    def test_update_user(self, userinfo):
        # 修改用户接口-正常
        base_url = datas.baseUrl
        url = base_url + userinfo['request']['url'].replace('{username}', globals()['username'])
        method = userinfo['request']['method']
        userinfo['request']['data']['Username'] = globals()['username']
        data = userinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        globals()['username'] = req.json()['Data']['Username']
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.usermanagement
    @pytest.mark.parametrize('userinfo', YamlUtil().read_testcase_yaml('v24.09/userManagement/2_get_user_detail.yaml'))
    def test_get_user_detail(self, userinfo):
        # 获取用户详情接口-正常
        base_url = datas.baseUrl
        url = base_url + userinfo['request']['url'].replace('{username}', globals()['username'])
        method = userinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.usermanagement
    @pytest.mark.parametrize('userinfo',
                             YamlUtil().read_testcase_yaml('v24.09/userManagement/5_get_platform_role.yaml'))
    def test_get_cluster_user_list(self, userinfo):
        # 获取平台级角色列表接口-正常
        base_url = datas.baseUrl
        url = base_url + userinfo['request']['url']
        method = userinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.usermanagement
    @pytest.mark.parametrize('userinfo', YamlUtil().read_testcase_yaml('v24.09/userManagement/6_get_cluster_role.yaml'))
    def test_get_cluster_user_list(self, userinfo):
        # 获取集群级角色列表接口-正常
        base_url = datas.baseUrl
        url = base_url + userinfo['request']['url']
        method = userinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.usermanagement
    @pytest.mark.parametrize('userinfo',
                             YamlUtil().read_testcase_yaml('v24.09/userManagement/7_get_cluster_user_list.yaml'))
    def test_get_cluster_user_list(self, userinfo):
        # 获取集群成员列表接口-正常
        base_url = datas.baseUrl
        url = base_url + userinfo['request']['url']
        method = userinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.usermanagement
    @pytest.mark.parametrize('userinfo',
                             YamlUtil().read_testcase_yaml('v24.09/userManagement/8_get_invite_user_list.yaml'))
    def test_get_invite_user_list(self, userinfo):
        # 获取可以被邀请至某个集群的用户列表接口-正常
        base_url = datas.baseUrl
        url = base_url + userinfo['request']['url'] + '?cluster-name=host'
        method = userinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.usermanagement
    @pytest.mark.parametrize('userinfo',
                             YamlUtil().read_testcase_yaml('v24.09/userManagement/9_invite_user_to_cluster.yaml'))
    def test_invite_user_to_cluster(self, userinfo):
        # 邀请用户到集群接口-正常
        base_url = datas.baseUrl
        urlinfo = datas.test_usermanagement
        replacements = {'user-name': globals()['username'], 'cluster-name': urlinfo['cluster-name'],
                        'cluster-role': urlinfo['cluster-role']}
        url = base_url + f"{userinfo['request']['url'].format_map(replacements)}"
        method = userinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.usermanagement
    @pytest.mark.parametrize('userinfo',
                             YamlUtil().read_testcase_yaml('v24.09/userManagement/10_delete_user_from_cluster.yaml'))
    def test_delete_user_from_cluster(self, userinfo):
        # 把用户从集群中移除接口-正常
        base_url = datas.baseUrl
        urlinfo = datas.test_usermanagement
        replacements = {'user-name': globals()['username'], 'cluster-name': urlinfo['cluster-name']}
        url = base_url + f"{userinfo['request']['url'].format_map(replacements)}"
        method = userinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.usermanagement
    @pytest.mark.parametrize('userinfo', YamlUtil().read_testcase_yaml('v24.09/userManagement/11_delete_user.yaml'))
    def test_delete_user(self, userinfo):
        # 删除用户接口-正常
        base_url = datas.baseUrl
        url = base_url + userinfo['request']['url'].replace('{username}', globals()['username'])
        method = userinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']
